package com.asia5b.a5mall.member.adapter

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.widget.CheckBox
import android.widget.TextView
import com.asia5b.a5mall.member.R
import com.asia5b.a5mall.member.databinding.ItemFootAdapterBinding
import com.asia5b.a5mall.member.utils.FootAdapterListener
import com.asia5b.a5mall.middleware.model.member.FootModel
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.utils.WTimeUtils
import com.asia5b.a5mall.middleware.utils.startGoodsDetailActivity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class FootAdapter(list: ArrayList<FootModel>, var listener: FootAdapterListener) :
        BaseQuickAdapter<FootModel, BaseDataBindingHolder<ItemFootAdapterBinding>>(R.layout.item_foot_adapter, list) {
    var isShowDelete = false
    override fun convert(helper: BaseDataBindingHolder<ItemFootAdapterBinding>, item: FootModel) {
        val viewDataBinding = helper.dataBinding!!
        viewDataBinding.model = item
        viewDataBinding.isShowDelete = isShowDelete
        val tv_foot_time = helper.getView<TextView>(R.id.tv_foot_time)
        val time = WTimeUtils.format("${item.time * 1000}", WTimeUtils.text_yyyy_MM_dd)
        tv_foot_time.text = time

        var recyclerView = helper.getView<RecyclerView>(R.id.rv_footgoods)
        item.history?.run {
            recyclerView.layoutManager = LinearLayoutManager(context)
            val mAdapter = FootGoodsAdapter(this)
            mAdapter.setOnItemChildClickListener { adapter, view, position ->
                if (view.id == R.id.cb_foot_goods_check) {
                    item.notifyCheck()
                    listener.checkedChange()
                } else if (view.id == R.id.tv_foot_delete) {
                    listener.deleteItem(mAdapter.getItem(position) as AbsGoodsModel)
                } else if (view.id == R.id.sl_footgoods_gen) {
                   startGoodsDetailActivity(context,(mAdapter.getItem(position) as AbsGoodsModel).absGoodsId)
                }else if (view.id == R.id.iv_foot_addcart) {
                    listener.addCart(mAdapter.getItem(position) as AbsGoodsModel)
                }
            }
            mAdapter.isShowDelete = isShowDelete
            recyclerView.adapter = mAdapter
        }
        helper.getView<CheckBox>(R.id.cb_foot_check).setOnClickListener {
            item.history?.forEach {
                it.absGoodsCheckedOb.set(item.checkOB.get())
            }
            listener.checkedChange()
        }
    }
}